Follow-Me Project

Congratulations on reaching the final project of the Robotics Nanodegree!

Previously, you worked on the Semantic Segmentation lab where you built a deep learning network that locates a particular human target within an image. For this project, you will utilize what you implemented and learned from that lab and extend it to train a deep learning model that will allow a simulated quadcopter to follow around the person that it detects!

Most of the code below is similar to the lab with some minor modifications. You can start with your existing solution, and modify and improve upon it to train the best possible model for this task.

You can click on any of the following to quickly jump to that part of this notebook:

  1. Data Collection
  2. FCN Layers
  3. Build the Model
  4. Training
  5. Prediction
  6. Evaluation

Data Collection

We have provided you with a starting dataset for this project. Download instructions can be found in the README for this project's repo. Alternatively, you can collect additional data of your own to improve your model. Check out the "Collecting Data" section in the Project Lesson in the Classroom for more details!

In [2]:
import os
import glob
import sys
import tensorflow as tf

from scipy import misc
import numpy as np

from tensorflow.contrib.keras.python import keras
from tensorflow.contrib.keras.python.keras import layers, models

from tensorflow import image

from utils import scoring_utils
from utils.separable_conv2d import SeparableConv2DKeras, BilinearUpSampling2D
from utils import data_iterator
from utils import plotting_tools 
from utils import model_tools

FCN Layers

In the Classroom, we discussed the different layers that constitute a fully convolutional network (FCN). The following code will introduce you to the functions that you need to build your semantic segmentation model.

Separable Convolutions

The Encoder for your FCN will essentially require separable convolution layers, due to their advantages as explained in the classroom. The 1x1 convolution layer in the FCN, however, is a regular convolution. Implementations for both are provided below for your use. Each includes batch normalization with the ReLU activation function applied to the layers.

In [3]:
def separable_conv2d_batchnorm(input_layer, filters, strides=1):
    output_layer = SeparableConv2DKeras(filters=filters,kernel_size=3, strides=strides,
                             padding='same', activation='relu')(input_layer)
    
    output_layer = layers.BatchNormalization()(output_layer) 
    return output_layer

def conv2d_batchnorm(input_layer, filters, kernel_size=3, strides=1):
    output_layer = layers.Conv2D(filters=filters, kernel_size=kernel_size, strides=strides, 
                      padding='same', activation='relu')(input_layer)
    
    output_layer = layers.BatchNormalization()(output_layer) 
    return output_layer

Bilinear Upsampling

The following helper function implements the bilinear upsampling layer. Upsampling by a factor of 2 is generally recommended, but you can try out different factors as well. Upsampling is used in the decoder block of the FCN.

In [4]:
def bilinear_upsample(input_layer):
    output_layer = BilinearUpSampling2D((2,2))(input_layer)
    return output_layer

Build the Model

In the following cells, you will build an FCN to train a model to detect and locate the hero target within an image. The steps are:

  • Create an encoder_block
  • Create a decoder_block
  • Build the FCN consisting of encoder block(s), a 1x1 convolution, and decoder block(s). This step requires experimentation with different numbers of layers and filter sizes to build your model.

Encoder Block

Create an encoder block that includes a separable convolution layer using the separable_conv2d_batchnorm() function. The filters parameter defines the size or depth of the output layer. For example, 32 or 64.

In [5]:
def encoder_block(input_layer, filters, strides):
    
    # TODO Create a separable convolution layer using the separable_conv2d_batchnorm() function.
    output_layer = separable_conv2d_batchnorm(input_layer, filters, strides)
    
    return output_layer

Decoder Block

The decoder block is comprised of three parts:

  • A bilinear upsampling layer using the upsample_bilinear() function. The current recommended factor for upsampling is set to 2.
  • A layer concatenation step. This step is similar to skip connections. You will concatenate the upsampled small_ip_layer and the large_ip_layer.
  • Some (one or two) additional separable convolution layers to extract some more spatial information from prior layers.
In [6]:
def decoder_block(small_ip_layer, large_ip_layer, filters):
    
    # TODO Upsample the small input layer using the bilinear_upsample() function.
    upsample_ip_layer = bilinear_upsample(small_ip_layer)
    
    # TODO Concatenate the upsampled and large input layers using layers.concatenate
    concat_layers = layers.concatenate([upsample_ip_layer, large_ip_layer])
    
    # TODO Add some number of separable convolution layers
    output_layer = separable_conv2d_batchnorm(concat_layers, filters)
    output_layer = separable_conv2d_batchnorm(output_layer,  filters)
    
    return output_layer

Model

Now that you have the encoder and decoder blocks ready, go ahead and build your FCN architecture!

There are three steps:

  • Add encoder blocks to build the encoder layers. This is similar to how you added regular convolutional layers in your CNN lab.
  • Add a 1x1 Convolution layer using the conv2d_batchnorm() function. Remember that 1x1 Convolutions require a kernel and stride of 1.
  • Add decoder blocks for the decoder layers.
In [7]:
def fcn_model(inputs, num_classes):
    
    # TODO Add Encoder Blocks. 
    # Remember that with each encoder layer, the depth of your model (the number of filters) increases.
    eb1 = encoder_block(inputs, 32, 2)
    eb2 = encoder_block(eb1, 64, 2)
    eb3 = encoder_block(eb2, 128, 2)

    # TODO Add 1x1 Convolution layer using conv2d_batchnorm().
    conv1x1 = conv2d_batchnorm(eb3, 512, kernel_size=1)
    
    # TODO: Add the same number of Decoder Blocks as the number of Encoder Blocks
    db1 = decoder_block(conv1x1, eb2, 128)
    db2 = decoder_block(db1, eb1, 64)
    db3 = decoder_block(db2, inputs, 32)
    
    # The function returns the output layer of your model. "x" is the final layer obtained from the last decoder_block()
    return layers.Conv2D(num_classes, 3, activation='softmax', padding='same')(db3)

Training

The following cells will use the FCN you created and define an ouput layer based on the size of the processed image and the number of classes recognized. You will define the hyperparameters to compile and train your model.

Please Note: For this project, the helper code in data_iterator.py will resize the copter images to 160x160x3 to speed up training.

In [8]:
"""
DON'T MODIFY ANYTHING IN THIS CELL THAT IS BELOW THIS LINE
"""

image_hw = 160
image_shape = (image_hw, image_hw, 3)
inputs = layers.Input(image_shape)
num_classes = 3

# Call fcn_model()
output_layer = fcn_model(inputs, num_classes)

Hyperparameters

Define and tune your hyperparameters.

  • batch_size: number of training samples/images that get propagated through the network in a single pass.
  • num_epochs: number of times the entire training dataset gets propagated through the network.
  • steps_per_epoch: number of batches of training images that go through the network in 1 epoch. We have provided you with a default value. One recommended value to try would be based on the total number of images in training dataset divided by the batch_size.
  • validation_steps: number of batches of validation images that go through the network in 1 epoch. This is similar to steps_per_epoch, except validation_steps is for the validation dataset. We have provided you with a default value for this as well.
  • workers: maximum number of processes to spin up. This can affect your training speed and is dependent on your hardware. We have provided a recommended value to work with.
In [22]:
learning_rate = 0.001
batch_size = 32
num_epochs = 100
steps_per_epoch = 130
validation_steps = 30
workers = 2
In [23]:
"""
DON'T MODIFY ANYTHING IN THIS CELL THAT IS BELOW THIS LINE
"""
# Define the Keras model and compile it for training
model = models.Model(inputs=inputs, outputs=output_layer)

model.compile(optimizer=keras.optimizers.Adam(learning_rate), loss='categorical_crossentropy')

# Data iterators for loading the training and validation data
train_iter = data_iterator.BatchIteratorSimple(batch_size=batch_size,
                                               data_folder=os.path.join('..', 'data', 'train_combined'),
                                               image_shape=image_shape,
                                               shift_aug=True)

val_iter = data_iterator.BatchIteratorSimple(batch_size=batch_size,
                                             data_folder=os.path.join('..', 'data', 'validation'),
                                             image_shape=image_shape)

logger_cb = plotting_tools.LoggerPlotter()
callbacks = [logger_cb]

model.fit_generator(train_iter,
                    steps_per_epoch = steps_per_epoch, # the number of batches per epoch,
                    epochs = num_epochs, # the number of epochs to train for,
                    validation_data = val_iter, # validation iterator
                    validation_steps = validation_steps, # the number of batches to validate on
                    callbacks=callbacks,
                    workers = workers)
Epoch 1/100
129/130 [============================>.] - ETA: 0s - loss: 0.0139
130/130 [==============================] - 84s - loss: 0.0138 - val_loss: 0.0234
Epoch 2/100
129/130 [============================>.] - ETA: 0s - loss: 0.0142
130/130 [==============================] - 83s - loss: 0.0142 - val_loss: 0.0360
Epoch 3/100
129/130 [============================>.] - ETA: 0s - loss: 0.0167
130/130 [==============================] - 83s - loss: 0.0167 - val_loss: 0.0388
Epoch 4/100
129/130 [============================>.] - ETA: 0s - loss: 0.0137
130/130 [==============================] - 83s - loss: 0.0136 - val_loss: 0.0339
Epoch 5/100
129/130 [============================>.] - ETA: 0s - loss: 0.0129
130/130 [==============================] - 83s - loss: 0.0129 - val_loss: 0.0325
Epoch 6/100
129/130 [============================>.] - ETA: 0s - loss: 0.0126
130/130 [==============================] - 83s - loss: 0.0126 - val_loss: 0.0344
Epoch 7/100
129/130 [============================>.] - ETA: 0s - loss: 0.0127
130/130 [==============================] - 83s - loss: 0.0127 - val_loss: 0.0314
Epoch 8/100
129/130 [============================>.] - ETA: 0s - loss: 0.0126
130/130 [==============================] - 83s - loss: 0.0127 - val_loss: 0.0369
Epoch 9/100
129/130 [============================>.] - ETA: 0s - loss: 0.0124
130/130 [==============================] - 83s - loss: 0.0125 - val_loss: 0.0429
Epoch 10/100
129/130 [============================>.] - ETA: 0s - loss: 0.0152
130/130 [==============================] - 83s - loss: 0.0151 - val_loss: 0.0331
Epoch 11/100
129/130 [============================>.] - ETA: 0s - loss: 0.0126
130/130 [==============================] - 83s - loss: 0.0125 - val_loss: 0.0343
Epoch 12/100
129/130 [============================>.] - ETA: 0s - loss: 0.0114
130/130 [==============================] - 83s - loss: 0.0115 - val_loss: 0.0357
Epoch 13/100
129/130 [============================>.] - ETA: 0s - loss: 0.0112
130/130 [==============================] - 83s - loss: 0.0112 - val_loss: 0.0334
Epoch 14/100
129/130 [============================>.] - ETA: 0s - loss: 0.0148
130/130 [==============================] - 83s - loss: 0.0148 - val_loss: 0.0320
Epoch 15/100
129/130 [============================>.] - ETA: 0s - loss: 0.0132
130/130 [==============================] - 83s - loss: 0.0132 - val_loss: 0.0313
Epoch 16/100
129/130 [============================>.] - ETA: 0s - loss: 0.0136
130/130 [==============================] - 83s - loss: 0.0137 - val_loss: 0.0380
Epoch 17/100
129/130 [============================>.] - ETA: 0s - loss: 0.0128
130/130 [==============================] - 83s - loss: 0.0131 - val_loss: 0.0301
Epoch 18/100
129/130 [============================>.] - ETA: 0s - loss: 0.0157
130/130 [==============================] - 83s - loss: 0.0156 - val_loss: 0.0307
Epoch 19/100
129/130 [============================>.] - ETA: 0s - loss: 0.0126
130/130 [==============================] - 83s - loss: 0.0127 - val_loss: 0.0379
Epoch 20/100
129/130 [============================>.] - ETA: 0s - loss: 0.0130
130/130 [==============================] - 83s - loss: 0.0130 - val_loss: 0.0223
Epoch 21/100
129/130 [============================>.] - ETA: 0s - loss: 0.0110
130/130 [==============================] - 83s - loss: 0.0111 - val_loss: 0.0332
Epoch 22/100
129/130 [============================>.] - ETA: 0s - loss: 0.0107
130/130 [==============================] - 83s - loss: 0.0107 - val_loss: 0.0307
Epoch 23/100
129/130 [============================>.] - ETA: 0s - loss: 0.0106
130/130 [==============================] - 83s - loss: 0.0106 - val_loss: 0.0206
Epoch 24/100
129/130 [============================>.] - ETA: 0s - loss: 0.0104
130/130 [==============================] - 83s - loss: 0.0104 - val_loss: 0.0365
Epoch 25/100
129/130 [============================>.] - ETA: 0s - loss: 0.0116
130/130 [==============================] - 83s - loss: 0.0116 - val_loss: 0.0231
Epoch 26/100
129/130 [============================>.] - ETA: 0s - loss: 0.0105
130/130 [==============================] - 83s - loss: 0.0104 - val_loss: 0.0313
Epoch 27/100
129/130 [============================>.] - ETA: 0s - loss: 0.0102
130/130 [==============================] - 84s - loss: 0.0102 - val_loss: 0.0339
Epoch 28/100
129/130 [============================>.] - ETA: 0s - loss: 0.0105
130/130 [==============================] - 83s - loss: 0.0107 - val_loss: 0.0397
Epoch 29/100
129/130 [============================>.] - ETA: 0s - loss: 0.0142
130/130 [==============================] - 83s - loss: 0.0142 - val_loss: 0.0273
Epoch 30/100
129/130 [============================>.] - ETA: 0s - loss: 0.0137
130/130 [==============================] - 83s - loss: 0.0137 - val_loss: 0.0316
Epoch 31/100
129/130 [============================>.] - ETA: 0s - loss: 0.0108
130/130 [==============================] - 83s - loss: 0.0108 - val_loss: 0.0338
Epoch 32/100
129/130 [============================>.] - ETA: 0s - loss: 0.0100
130/130 [==============================] - 83s - loss: 0.0100 - val_loss: 0.0223
Epoch 33/100
129/130 [============================>.] - ETA: 0s - loss: 0.0098
130/130 [==============================] - 83s - loss: 0.0098 - val_loss: 0.0458
Epoch 34/100
129/130 [============================>.] - ETA: 0s - loss: 0.0098
130/130 [==============================] - 83s - loss: 0.0098 - val_loss: 0.0223
Epoch 35/100
129/130 [============================>.] - ETA: 0s - loss: 0.0101
130/130 [==============================] - 83s - loss: 0.0101 - val_loss: 0.0355
Epoch 36/100
129/130 [============================>.] - ETA: 0s - loss: 0.0097
130/130 [==============================] - 84s - loss: 0.0097 - val_loss: 0.0270
Epoch 37/100
129/130 [============================>.] - ETA: 0s - loss: 0.0094
130/130 [==============================] - 83s - loss: 0.0095 - val_loss: 0.0303
Epoch 38/100
129/130 [============================>.] - ETA: 0s - loss: 0.0095
130/130 [==============================] - 83s - loss: 0.0095 - val_loss: 0.0379
Epoch 39/100
129/130 [============================>.] - ETA: 0s - loss: 0.0139
130/130 [==============================] - 84s - loss: 0.0139 - val_loss: 0.0369
Epoch 40/100
129/130 [============================>.] - ETA: 0s - loss: 0.0112
130/130 [==============================] - 83s - loss: 0.0112 - val_loss: 0.0447
Epoch 41/100
129/130 [============================>.] - ETA: 0s - loss: 0.0111
130/130 [==============================] - 83s - loss: 0.0111 - val_loss: 0.0248
Epoch 42/100
129/130 [============================>.] - ETA: 0s - loss: 0.0105
130/130 [==============================] - 83s - loss: 0.0105 - val_loss: 0.0211
Epoch 43/100
129/130 [============================>.] - ETA: 0s - loss: 0.0104
130/130 [==============================] - 83s - loss: 0.0104 - val_loss: 0.0215
Epoch 44/100
129/130 [============================>.] - ETA: 0s - loss: 0.0094
130/130 [==============================] - 83s - loss: 0.0096 - val_loss: 0.0436
Epoch 45/100
129/130 [============================>.] - ETA: 0s - loss: 0.0103
130/130 [==============================] - 83s - loss: 0.0103 - val_loss: 0.0212
Epoch 46/100
129/130 [============================>.] - ETA: 0s - loss: 0.0095
130/130 [==============================] - 83s - loss: 0.0095 - val_loss: 0.0480
Epoch 47/100
129/130 [============================>.] - ETA: 0s - loss: 0.0091
130/130 [==============================] - 84s - loss: 0.0091 - val_loss: 0.0260
Epoch 48/100
129/130 [============================>.] - ETA: 0s - loss: 0.0090
130/130 [==============================] - 83s - loss: 0.0090 - val_loss: 0.0318
Epoch 49/100
129/130 [============================>.] - ETA: 0s - loss: 0.0089
130/130 [==============================] - 83s - loss: 0.0089 - val_loss: 0.0450
Epoch 50/100
129/130 [============================>.] - ETA: 0s - loss: 0.0088
130/130 [==============================] - 83s - loss: 0.0089 - val_loss: 0.0351
Epoch 51/100
129/130 [============================>.] - ETA: 0s - loss: 0.0088
130/130 [==============================] - 84s - loss: 0.0088 - val_loss: 0.0244
Epoch 52/100
129/130 [============================>.] - ETA: 0s - loss: 0.0088
130/130 [==============================] - 83s - loss: 0.0088 - val_loss: 0.0489
Epoch 53/100
129/130 [============================>.] - ETA: 0s - loss: 0.0087
130/130 [==============================] - 83s - loss: 0.0087 - val_loss: 0.0391
Epoch 54/100
129/130 [============================>.] - ETA: 0s - loss: 0.0089
130/130 [==============================] - 83s - loss: 0.0089 - val_loss: 0.0405
Epoch 55/100
129/130 [============================>.] - ETA: 0s - loss: 0.0091
130/130 [==============================] - 84s - loss: 0.0091 - val_loss: 0.0365
Epoch 56/100
129/130 [============================>.] - ETA: 0s - loss: 0.0089
130/130 [==============================] - 83s - loss: 0.0090 - val_loss: 0.0407
Epoch 57/100
129/130 [============================>.] - ETA: 0s - loss: 0.0224
130/130 [==============================] - 83s - loss: 0.0224 - val_loss: 0.0460
Epoch 58/100
129/130 [============================>.] - ETA: 0s - loss: 0.0192
130/130 [==============================] - 84s - loss: 0.0192 - val_loss: 0.0220
Epoch 59/100
129/130 [============================>.] - ETA: 0s - loss: 0.0122
130/130 [==============================] - 83s - loss: 0.0123 - val_loss: 0.0320
Epoch 60/100
129/130 [============================>.] - ETA: 0s - loss: 0.0105
130/130 [==============================] - 83s - loss: 0.0105 - val_loss: 0.0214
Epoch 61/100
129/130 [============================>.] - ETA: 0s - loss: 0.0105
130/130 [==============================] - 83s - loss: 0.0105 - val_loss: 0.0346
Epoch 62/100
129/130 [============================>.] - ETA: 0s - loss: 0.0113
130/130 [==============================] - 83s - loss: 0.0114 - val_loss: 0.0329
Epoch 63/100
129/130 [============================>.] - ETA: 0s - loss: 0.0106
130/130 [==============================] - 83s - loss: 0.0106 - val_loss: 0.0300
Epoch 64/100
129/130 [============================>.] - ETA: 0s - loss: 0.0099
130/130 [==============================] - 83s - loss: 0.0100 - val_loss: 0.0227
Epoch 65/100
129/130 [============================>.] - ETA: 0s - loss: 0.0093
130/130 [==============================] - 83s - loss: 0.0093 - val_loss: 0.0365
Epoch 66/100
129/130 [============================>.] - ETA: 0s - loss: 0.0092
130/130 [==============================] - 83s - loss: 0.0092 - val_loss: 0.0318
Epoch 67/100
129/130 [============================>.] - ETA: 0s - loss: 0.0090
130/130 [==============================] - 83s - loss: 0.0089 - val_loss: 0.0226
Epoch 68/100
129/130 [============================>.] - ETA: 0s - loss: 0.0087
130/130 [==============================] - 83s - loss: 0.0087 - val_loss: 0.0488
Epoch 69/100
129/130 [============================>.] - ETA: 0s - loss: 0.0086
130/130 [==============================] - 83s - loss: 0.0086 - val_loss: 0.0240
Epoch 70/100
129/130 [============================>.] - ETA: 0s - loss: 0.0085
130/130 [==============================] - 83s - loss: 0.0086 - val_loss: 0.0394
Epoch 71/100
129/130 [============================>.] - ETA: 0s - loss: 0.0096
130/130 [==============================] - 83s - loss: 0.0096 - val_loss: 0.0254
Epoch 72/100
129/130 [============================>.] - ETA: 0s - loss: 0.0087
130/130 [==============================] - 83s - loss: 0.0087 - val_loss: 0.0345
Epoch 73/100
129/130 [============================>.] - ETA: 0s - loss: 0.0085
130/130 [==============================] - 83s - loss: 0.0085 - val_loss: 0.0247
Epoch 74/100
129/130 [============================>.] - ETA: 0s - loss: 0.0084
130/130 [==============================] - 84s - loss: 0.0084 - val_loss: 0.0377
Epoch 75/100
129/130 [============================>.] - ETA: 0s - loss: 0.0084
130/130 [==============================] - 83s - loss: 0.0084 - val_loss: 0.0383
Epoch 76/100
129/130 [============================>.] - ETA: 0s - loss: 0.0099
130/130 [==============================] - 83s - loss: 0.0100 - val_loss: 0.0347
Epoch 77/100
129/130 [============================>.] - ETA: 0s - loss: 0.0100
130/130 [==============================] - 83s - loss: 0.0101 - val_loss: 0.0369
Epoch 78/100
129/130 [============================>.] - ETA: 0s - loss: 0.0106
130/130 [==============================] - 84s - loss: 0.0105 - val_loss: 0.0343
Epoch 79/100
129/130 [============================>.] - ETA: 0s - loss: 0.0091
130/130 [==============================] - 83s - loss: 0.0092 - val_loss: 0.0355
Epoch 80/100
129/130 [============================>.] - ETA: 0s - loss: 0.0108
130/130 [==============================] - 83s - loss: 0.0107 - val_loss: 0.0344
Epoch 81/100
129/130 [============================>.] - ETA: 0s - loss: 0.0092
130/130 [==============================] - 83s - loss: 0.0092 - val_loss: 0.0553
Epoch 82/100
129/130 [============================>.] - ETA: 0s - loss: 0.0110
130/130 [==============================] - 83s - loss: 0.0110 - val_loss: 0.0347
Epoch 83/100
129/130 [============================>.] - ETA: 0s - loss: 0.0101
130/130 [==============================] - 83s - loss: 0.0101 - val_loss: 0.0326
Epoch 84/100
129/130 [============================>.] - ETA: 0s - loss: 0.0087
130/130 [==============================] - 84s - loss: 0.0088 - val_loss: 0.0231
Epoch 85/100
129/130 [============================>.] - ETA: 0s - loss: 0.0085
130/130 [==============================] - 84s - loss: 0.0085 - val_loss: 0.0446
Epoch 86/100
129/130 [============================>.] - ETA: 0s - loss: 0.0083
130/130 [==============================] - 83s - loss: 0.0083 - val_loss: 0.0402
Epoch 87/100
129/130 [============================>.] - ETA: 0s - loss: 0.0083
130/130 [==============================] - 83s - loss: 0.0083 - val_loss: 0.0241
Epoch 88/100
129/130 [============================>.] - ETA: 0s - loss: 0.0082
130/130 [==============================] - 83s - loss: 0.0082 - val_loss: 0.0367
Epoch 89/100
129/130 [============================>.] - ETA: 0s - loss: 0.0082
130/130 [==============================] - 83s - loss: 0.0082 - val_loss: 0.0229
Epoch 90/100
129/130 [============================>.] - ETA: 0s - loss: 0.0081
130/130 [==============================] - 83s - loss: 0.0082 - val_loss: 0.0241
Epoch 91/100
129/130 [============================>.] - ETA: 0s - loss: 0.0082
130/130 [==============================] - 83s - loss: 0.0082 - val_loss: 0.0508
Epoch 92/100
129/130 [============================>.] - ETA: 0s - loss: 0.0081
130/130 [==============================] - 83s - loss: 0.0081 - val_loss: 0.0357
Epoch 93/100
129/130 [============================>.] - ETA: 0s - loss: 0.0080
130/130 [==============================] - 83s - loss: 0.0080 - val_loss: 0.0269
Epoch 94/100
129/130 [============================>.] - ETA: 0s - loss: 0.0079
130/130 [==============================] - 83s - loss: 0.0079 - val_loss: 0.0524
Epoch 95/100
129/130 [============================>.] - ETA: 0s - loss: 0.0090
130/130 [==============================] - 83s - loss: 0.0090 - val_loss: 0.0233
Epoch 96/100
129/130 [============================>.] - ETA: 0s - loss: 0.0086
130/130 [==============================] - 83s - loss: 0.0086 - val_loss: 0.0494
Epoch 97/100
129/130 [============================>.] - ETA: 0s - loss: 0.0126
130/130 [==============================] - 83s - loss: 0.0126 - val_loss: 0.0343
Epoch 98/100
129/130 [============================>.] - ETA: 0s - loss: 0.0094
130/130 [==============================] - 83s - loss: 0.0094 - val_loss: 0.0377
Epoch 99/100
129/130 [============================>.] - ETA: 0s - loss: 0.0097
130/130 [==============================] - 83s - loss: 0.0097 - val_loss: 0.0401
Epoch 100/100
129/130 [============================>.] - ETA: 0s - loss: 0.0096
130/130 [==============================] - 83s - loss: 0.0096 - val_loss: 0.0346
Out[23]:
<tensorflow.contrib.keras.python.keras.callbacks.History at 0x7f8c99e69d68>
In [24]:
# Save your trained model weights
weight_file_name = 'model_weights'
model_tools.save_network(model, weight_file_name)

Prediction

Now that you have your model trained and saved, you can make predictions on your validation dataset. These predictions can be compared to the mask images, which are the ground truth labels, to evaluate how well your model is doing under different conditions.

There are three different predictions available from the helper code provided:

  • patrol_with_targ: Test how well the network can detect the hero from a distance.
  • patrol_non_targ: Test how often the network makes a mistake and identifies the wrong person as the target.
  • following_images: Test how well the network can identify the target while following them.
In [25]:
# If you need to load a model which you previously trained you can uncomment the codeline that calls the function below.

# weight_file_name = 'model_weights'
# restored_model = model_tools.load_network(weight_file_name)

The following cell will write predictions to files and return paths to the appropriate directories. The run_num parameter is used to define or group all the data for a particular model run. You can change it for different runs. For example, 'run_1', 'run_2' etc.

In [26]:
run_num = 'run_1'

val_with_targ, pred_with_targ = model_tools.write_predictions_grade_set(model,
                                        run_num,'patrol_with_targ', 'sample_evaluation_data') 

val_no_targ, pred_no_targ = model_tools.write_predictions_grade_set(model, 
                                        run_num,'patrol_non_targ', 'sample_evaluation_data') 

val_following, pred_following = model_tools.write_predictions_grade_set(model,
                                        run_num,'following_images', 'sample_evaluation_data')

Now lets look at your predictions, and compare them to the ground truth labels and original images. Run each of the following cells to visualize some sample images from the predictions in the validation set.

In [27]:
# images while following the target
im_files = plotting_tools.get_im_file_sample('sample_evaluation_data','following_images', run_num) 
for i in range(3):
    im_tuple = plotting_tools.load_images(im_files[i])
    plotting_tools.show_images(im_tuple)
    
In [28]:
# images while at patrol without target
im_files = plotting_tools.get_im_file_sample('sample_evaluation_data','patrol_non_targ', run_num) 
for i in range(3):
    im_tuple = plotting_tools.load_images(im_files[i])
    plotting_tools.show_images(im_tuple)
 
In [29]:
   
# images while at patrol with target
im_files = plotting_tools.get_im_file_sample('sample_evaluation_data','patrol_with_targ', run_num) 
for i in range(3):
    im_tuple = plotting_tools.load_images(im_files[i])
    plotting_tools.show_images(im_tuple)

Evaluation

Evaluate your model! The following cells include several different scores to help you evaluate your model under the different conditions discussed during the Prediction step.

In [30]:
# Scores for while the quad is following behind the target. 
true_pos1, false_pos1, false_neg1, iou1 = scoring_utils.score_run_iou(val_following, pred_following)
number of validation samples intersection over the union evaulated on 542
average intersection over union for background is 0.9959755480827662
average intersection over union for other people is 0.37023855620037877
average intersection over union for the hero is 0.9164995499716416
number true positives: 539, number false positives: 0, number false negatives: 0
In [31]:
# Scores for images while the quad is on patrol and the target is not visable
true_pos2, false_pos2, false_neg2, iou2 = scoring_utils.score_run_iou(val_no_targ, pred_no_targ)
number of validation samples intersection over the union evaulated on 270
average intersection over union for background is 0.9867890052793926
average intersection over union for other people is 0.7393643795251216
average intersection over union for the hero is 0.0
number true positives: 0, number false positives: 48, number false negatives: 0
In [32]:
# This score measures how well the neural network can detect the target from far away
true_pos3, false_pos3, false_neg3, iou3 = scoring_utils.score_run_iou(val_with_targ, pred_with_targ)
number of validation samples intersection over the union evaulated on 322
average intersection over union for background is 0.9965618221920743
average intersection over union for other people is 0.45381986453618456
average intersection over union for the hero is 0.21192313347105302
number true positives: 124, number false positives: 2, number false negatives: 177
In [33]:
# Sum all the true positives, etc from the three datasets to get a weight for the score
true_pos = true_pos1 + true_pos2 + true_pos3
false_pos = false_pos1 + false_pos2 + false_pos3
false_neg = false_neg1 + false_neg2 + false_neg3

weight = true_pos/(true_pos+false_neg+false_pos)
print(weight)
0.744943820224719
In [34]:
# The IoU for the dataset that never includes the hero is excluded from grading
final_IoU = (iou1 + iou3)/2
print(final_IoU)
0.564211341721
In [35]:
# And the final grade score is 
final_score = final_IoU * weight
print(final_score)
0.420305752316